import request from '@/utils/request'
// import { getUser, getToken } from '@/utils/auth'
import qs from 'qs'
import sha1 from 'sha1'

export function login(data) {
  return request({
    url: '/login',
    headers: { 'content-type': 'application/x-www-form-urlencoded' },
    method: 'post',
    data: qs.stringify(data)
  })
}

export function getInfo(token) {
  // 临时使用Cookie来模拟数据了
  // return new Promise((resolve, reject) => {
  //   if (token === getToken()) {
  //     const user = getUser()
  //     if (!user.roles) {
  //       user.roles = [user.roleId === 1 ? 'admin' : 'editor']
  //     }
  //     const ret = {
  //       roles: user.roles,
  //       name: user.userName,
  //       avatar: user.avatarUrl ? user.avatarUrl : 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
  //       introduction: user.uuid
  //     }
  //     console.log(111, ret, 222, user)
  //     resolve({ code: 200, data: ret })
  //   } else {
  //     reject('认证失败，请重新登录')
  //   }
  // })
  return request({
    url: '/user/getUser',
    method: 'get',
    params: { }
  })
}

export function logout() {
  return request({
    url: '/logout',
    method: 'post'
  })
}

// 分页获取所有用户
export function getUserList(data) {
  return request({
    url: '/user/all',
    method: 'post',
    data
  })
}

// 添加用户
export function addUser(data) {
  const req = Object.assign({}, data)
  req.password = sha1(data.password)
  return request({
    url: '/user/add',
    method: 'post',
    data: req
  })
}

// 修改用户
export function putUser(data) {
  return request({
    url: '/user/edit',
    method: 'post',
    data
  })
}

// 修改密码
export function changePassword(data) {
  const req = Object.assign({}, data)
  req.newPassword = sha1(data.newPassword)
  req.curPassword = sha1(data.curPassword)
  return request({
    url: '/user/editPass',
    method: 'post',
    data: req
  })
}

// 重置密码
export function resetPassword(data) {
  const req = Object.assign({}, data)
  req.password = sha1(data.password)
  return request({
    url: '/reset/pass/' + data.id,
    method: 'post',
    data: req
  })
}

// 删除用户
export function delUser(data) {
  return request({
    url: '/del/' + data.userId,
    method: 'delete',
    headers: { 'content-type': 'application/x-www-form-urlencoded' },
    data: qs.stringify(data)
  })
}
